//
//  JHExplainInfoViewCell.m
//  JuniRengLish_iOS
//
//  Created by Maiyou on 2019/5/23.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHExplainInfoViewCell.h"

@implementation JHExplainInfoViewCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.contentView.layer.cornerRadius = 10;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.backgroundColor = JKColorAlpha(239, 239, 244, 0.4);
    
    [self.voiceButton1 setImage:[UIImage sd_animatedGIFNamed:@"music_play"] forState:UIControlStateSelected];
    [self.voiceButton2 setImage:[UIImage sd_animatedGIFNamed:@"music_play"] forState:UIControlStateSelected];
    [self.voiceButton3 setImage:[UIImage sd_animatedGIFNamed:@"music_play"] forState:UIControlStateSelected];
    
    [self.voiceButton1 layoutButtonWithImageStyle:ZJButtonImageStyleRight imageTitleToSpace:5];
    [self.voiceButton2 layoutButtonWithImageStyle:ZJButtonImageStyleRight imageTitleToSpace:5];
    [self.voiceButton3 layoutButtonWithImageStyle:ZJButtonImageStyleRight imageTitleToSpace:5];
    
    IXAttributeTapLabel * label = [[IXAttributeTapLabel alloc] initWithFrame:CGRectMake(0, 0, self.contentView.width - 40, 30)];
//    label.backgroundColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    //文本点击回调
    label.tapBlock = ^(NSString *string) {
        NSLog(@" -- %@ --",string);
        [self playWord:string Lan:@"uk"];
        [self getTranferWordInfo:string];
    };
    [self.backView addSubview:label];
    self.label = label;
}

- (void)setLrcModel:(wslLrcEach *)lrcModel
{
    _lrcModel = lrcModel;
    
    if (![lrcModel.lrc containsString:@"\n"])
    {
        return;
    }
    NSArray * array = [lrcModel.lrc componentsSeparatedByString:@"\n"];
    
//    self.label.text = array[0];
    self.showContent2.text = array[1];
    
    NSArray * wordArray = [array[0] componentsSeparatedByString:@" "];
    NSMutableArray * allArray = [NSMutableArray array];
    for (int i = 0; i < wordArray.count; i ++)
    {
        NSString * string = wordArray[i];
        string = [string stringByReplacingOccurrencesOfString:@"." withString:@""];
        string = [string stringByReplacingOccurrencesOfString:@"," withString:@""];
        string = [string stringByReplacingOccurrencesOfString:@"!" withString:@""];
        string = [string stringByReplacingOccurrencesOfString:@"?" withString:@""];
        
        if ([string containsString:@":"])
        {
            NSArray * sepArray = [string componentsSeparatedByString:@":"];
            for (NSString * word in sepArray)
            {
                IXAttributeModel    * model1 = [IXAttributeModel new];
                model1.range = [array[0] rangeOfString:word];
                model1.string = word;
                model1.attributeDic = @{NSForegroundColorAttributeName : [UIColor blueColor]};
                [allArray addObject:model1];
            }
        }
        else
        {
            IXAttributeModel    * model1 = [IXAttributeModel new];
            model1.range = [array[0] rangeOfString:string];
            model1.string = string;
            model1.attributeDic = @{NSForegroundColorAttributeName : [UIColor blueColor]};
            [allArray addObject:model1];
        }
    }
    //label内容赋值
    [self.label setText:array[0] attributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:15]}
    tapStringArray:allArray];
    
    CGSize attSize = [self.label.attributedText boundingRectWithSize:CGSizeMake(self.label.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    self.label.height = attSize.height + 10;
    self.showContent1_height.constant = attSize.height + 10;
}

- (void)getTranferWordInfo:(NSString *)word
{
    [DJHttpTool postWithURL:@"http://dict.youdao.com/jsonresult" params:@{@"q":word, @"pos":@"yidu", @"type":@"1"} success:^(id json) {
        self.explainView.dataDic = json;
    } failure:^(NSError *error) {
        
    }];
    NSLog(@"hello world");
}

- (JHExplainInfoView *)explainView
{
    if (!_explainView)
    {
        _explainView = [[JHExplainInfoView alloc] initWithFrame:CGRectMake(0, 0, JKScreenW, JKScreenH)];
        _explainView.currentVC = self.currentVC;
        [[UIApplication sharedApplication].delegate.window addSubview:_explainView];
    }
    return _explainView;
}

- (FSAudioStream *)audioStream
{
    if (!_audioStream)
    {
        WEAKSELF
        //创建FSAudioStream对象
        _audioStream = [[FSAudioStream alloc]init];
        _audioStream.onFailure = ^(FSAudioStreamError error,NSString *description){
            NSLog(@"播放过程中发生错误，错误信息：%@",description);
        };
        _audioStream.onCompletion = ^(){
            NSLog(@"播放完成!");
            weakSelf.selectedButton.selected = NO;
        };
        _audioStream.onStateChange = ^(FSAudioStreamState state) {
            
        };
        [_audioStream setVolume:1];//设置声音
    }
    return _audioStream;
}

- (IBAction)voiceButtonClick:(id)sender
{
    UIButton * button = sender;
    button.selected = YES;
    self.selectedButton = button;
    switch (button.tag) {
        case 10:
            [self playWord:self.label.attributedText.string Lan:@"en"];
            break;
        case 11:
            [self playWord:self.label.attributedText.string Lan:@"uk"];
            break;
        case 12:
            [self playWord:self.showContent2.text Lan:@"zh"];
            break;
            
        default:
            break;
    }
}

- (void)playWord:(NSString *)word Lan:(NSString *)lan
{
    word = [word stringByAddingPercentEncodingWithAllowedCharacters:  [NSCharacterSet URLUserAllowedCharacterSet]];
    NSString * urlStr = [NSString stringWithFormat:@"https://fanyi.baidu.com/gettts?lan=%@&text=%@&spd=3&source=web", lan, word];
    self.audioStream.url = [NSURL URLWithString:urlStr];
    [self.audioStream play];
}

@end
